from django.db import models

# Create your models here.
# 品牌表
class Brand(models.Model):
    first_name = models.CharField('品牌首字母', max_length=10, default=None)
    name = models.CharField('品牌名称',max_length=20)
    logo = models.CharField('品牌logo',max_length=200)

    class Meta:
        db_table = 'brand'
        verbose_name_plural = '品牌'

    def __str__(self):
        return self.name


# 频道组的管理
class Cate(models.Model):
    name = models.CharField('种类名称',max_length=30)
    # 自关联
    cid = models.ForeignKey('self',on_delete=models.CASCADE,related_name='subs',null=True,blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'cate'
        verbose_name_plural = '商品种类'


# 频道组分组
class Group(models.Model):
    name = models.CharField('频道组名称',max_length=30)
    # 一对一的引用关系，on_delete级联删除
    cate = models.OneToOneField(Cate,on_delete=models.CASCADE)
    group_url = models.CharField("频道组的连接",max_length=100,default='http://jd.com')
    sequence = models.IntegerField('频道组的展示顺序',default=0)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'cate_group'
        verbose_name_plural = '频道组'


# SPU
class SPU(models.Model):
    name = models.CharField('spu',max_length=20)
    brand = models.ForeignKey(Brand,on_delete=models.CASCADE,verbose_name='品牌')
    category1 = models.ForeignKey(Cate,on_delete=models.PROTECT,related_name='cate1_spu',verbose_name='一级类别')
    category2 = models.ForeignKey(Cate,on_delete=models.PROTECT,related_name='cate2_spu',verbose_name='二级类别')
    category3 = models.ForeignKey(Cate,on_delete=models.PROTECT,related_name='cate3_spu',verbose_name='三级类别')
    comment_num = models.IntegerField('评论数',default=0)
    sales = models.IntegerField('销量',default=0)
    desc_detail = models.TextField('详情描述',default='')
    desc_pack = models.TextField('包装描述',default='')
    desc_service = models.TextField('售后服务',default='')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'spu'
        verbose_name_plural = 'SPU表'


# SKU表
class Good(models.Model):
    name = models.CharField('商品名',max_length=100)
    describe = models.CharField('商品描述',max_length=500)
    price = models.DecimalField('商品价格',max_digits=8,decimal_places=2)
    stock = models.IntegerField('库存',default=0)
    sales = models.IntegerField('销量',default=0)
    detail = models.TextField('商品详情',default='')
    image_default = models.CharField('默认图片',max_length=300)
    is_selling = models.BooleanField('是否在售',default=True)
    # 引用一级类别
    cate = models.ForeignKey(Cate,on_delete=models.CASCADE,verbose_name='商品类别')
    spu = models.ForeignKey(SPU,on_delete=models.CASCADE,verbose_name='商品的SPU')
    def __str__(self):
        return self.name

    class Meta:
        db_table = 'sku'
        verbose_name_plural = '商品SKU'


# SPU规格模型类
class SPUSpec(models.Model):
    name = models.CharField('规格名称',max_length=30)
    spu = models.ForeignKey(SPU,on_delete=models.CASCADE,verbose_name='商品spu')

    class Meta:
        db_table = 'tb_spuspec'
        verbose_name_plural = 'SPU 规格表'

    def __str__(self):
        return self.name


# 规格选项值
class SpecOption(models.Model):
    value = models.CharField('选项值',max_length=20)
    spec = models.ForeignKey(SPUSpec,on_delete=models.CASCADE,verbose_name='规格')
    class Meta:
        db_table = 'spec_option'

    def __str__(self):
        return '%s:%s'%(self.value,self.spec.name)


# SKU的规格
class SKUSpecOption(models.Model):
    sku = models.ForeignKey(Good,on_delete=models.CASCADE,verbose_name='SKU商品')
    spec = models.ForeignKey(SPUSpec,on_delete=models.CASCADE,verbose_name='规格名称')
    option = models.ForeignKey(SpecOption,on_delete=models.CASCADE,verbose_name='规格选项值')

    def __str__(self):
        return '%s:%s-%s'%(self.sku.name,self.spec.name,self.option.value)

    class Meta:
        db_table = 'sku_spec_option'
        verbose_name_plural = '商品规格'


# 图片表
class SKUImage(models.Model):
    image = models.CharField('商品图片',max_length=300)
    sku = models.ForeignKey(Good,on_delete=models.CASCADE,verbose_name='SKU商品')

    def __str__(self):
        return "%s:%s"%(self.sku.name,self.image)

    class Meta:
        db_table = 'sku_image'
        verbose_name_plural = 'SKU图片'


